//
//  UIImage+DebugManager.m
//  YK_DebugManager
//
//  Created by edward on 2021/1/18.
//

#import "UIImage+DebugManager.h"

@implementation UIImage (DebugManager)
+ (nullable UIImage *)debugmanager_local_imageNamed:(NSString *)name{
    if(name &&
       ![name isEqualToString:@""]){
        NSBundle *bundle = [NSBundle bundleForClass:NSClassFromString(@"YKDebugManager")];
        
        NSString *imageName = nil;
        CGFloat scale = [UIScreen mainScreen].scale;
        if (ABS(scale-3) <= 0.001){
            imageName = [NSString stringWithFormat:@"%@@3x",name];
        }else if(ABS(scale-2) <= 0.001){
            imageName = [NSString stringWithFormat:@"%@@2x",name];
        }else{
            imageName = name;
        }
        UIImage *image = [UIImage imageWithContentsOfFile:[bundle pathForResource:imageName ofType:@"png"]];
        if (!image) {
            image = [UIImage imageWithContentsOfFile:[bundle pathForResource:name ofType:@"png"]];
            if (!image) {
                image = [UIImage imageNamed:name];
            }
        }
        return image;
    }
    
    return nil;
}

+ (nullable UIImage *)debugmanager_xcassetImageNamed:(NSString *)name {
    if(name &&
       ![name isEqualToString:@""]){
        NSBundle *bundle = [NSBundle bundleForClass:NSClassFromString(@"YKDebugManager")];
        UIImage *image = [UIImage imageNamed:name inBundle:bundle compatibleWithTraitCollection:nil];
        return image;
    }
    
    return nil;
}

+ (nullable UIImage *)debugmanager_imageNamed:(NSString *)name {
    
    UIImage *image = [UIImage debugmanager_xcassetImageNamed:name];
    
    if (image == nil) {
        image = [UIImage debugmanager_local_imageNamed:name];
    }
    
    if (image == nil) {
        image = [UIImage imageNamed:name];
    }
    
    return image;
    
}

@end
